home *** CD-ROM | disk | FTP | other *** search
/ The Utilities Experience / The Utilities Experience - Volume 1.iso / software / graphics / a-g / beyondthedark / developer / source / storm / storm.c < prev   
Encoding:
C/C++ Source or Header  |  1995-12-21  |  6.3 KB  |  306 lines

  1. /* Storm Library */
  2.  
  3. #include <exec/memory.h>
  4. #include <exec/execbase.h>
  5. #include <graphics/gfxbase.h>
  6. #include <intuition/intuitionbase.h>
  7. #include <libraries/iffparse.h>
  8. #include <utility/tagitem.h>
  9.  
  10. #include <clib/macros.h>
  11.  
  12. #define __USE_SYSBASE 42
  13.  
  14. #include <proto/exec.h>
  15. #include <proto/graphics.h>
  16. #include <proto/intuition.h>
  17.  
  18. #include <BTD.h>
  19.  
  20. struct IntuitionBase *IntuitionBase;
  21. struct GfxBase *GfxBase;
  22. struct Library *UtilityBase;
  23.  
  24. /* #define DEBUG YES */
  25.  
  26. #ifdef DEBUG 
  27.  
  28. void KPrintF(char *,...);
  29.  
  30. #define DEBUG_PRINTF(a,b)  KPrintF(a,b);
  31. #define DEBUG_PRINT(a)     KPrintF(a)
  32. #else
  33. #define DEBUG_PRINTF(a,b)
  34. #define DEBUG_PRINT(a)
  35. #endif
  36.  
  37. #define DTAG(o) (BTD_Client+(o))
  38.  
  39. #define RP_Drops DTAG(0)
  40. #define RP_Colors DTAG(1)
  41. #define RP_Mode  DTAG(2)
  42.  
  43. #define DEF_COLORS 15L
  44. #define MAX_COLORS 255L
  45.  
  46. #define DEF_DROPS 25L
  47. #define MAX_DROPS 100L
  48.  
  49. #define DEF_MODES 0L
  50. #define MAX_MODES 1L
  51.  
  52. #ifndef max
  53. #define max( x, y ) ((x)>(y)?(x):(y))
  54. #endif
  55.  
  56. char MyBlankerName[] = "storm.btd";
  57. char MyBlankerID[]   = "Storm Blanker V" VERSION "." REVISION " for BTD";
  58.  
  59. char *Modes[] = {"Stay Dark","Flash and Light",NULL};
  60.  
  61. struct BTDCycle StormCycleParams[] =
  62.  {
  63.   RP_Mode,"Mode",BTDPT_CYCLE,DEF_MODES,Modes
  64.  };
  65.  
  66. struct BTDInteger StormIntParams[] =
  67.  {
  68.   RP_Drops,"Drops",BTDPT_INTEGER,DEF_DROPS,1L,MAX_DROPS,TRUE,
  69.   RP_Colors,"Length/Colors",BTDPT_INTEGER,DEF_COLORS,1L,MAX_COLORS,TRUE
  70.  };
  71.  
  72. struct BTDNode *StormParams[] = 
  73.  {
  74.   &StormIntParams[0].BI_Node,
  75.   &StormIntParams[1].BI_Node,
  76.   &StormCycleParams[0].BC_Node,
  77.   NULL
  78.  };
  79.  
  80. struct BTDInfo StormInfo =
  81.  {
  82.   BTDI_Revision,MAKE_ID('S','T','O','R'),
  83.   "Stormy Night","See drops falling...","Markus Illenseer 1995",
  84.   StormParams
  85.  };
  86.  
  87. struct StormStruct
  88.  {
  89.   struct BTDDrawInfo *BTDDrawInfo;
  90.   LONG Colors;
  91.   LONG Drops;
  92.   LONG DropCount;
  93.   LONG Mode;
  94.   LONG RandN,RandF,RandI;
  95.   BYTE Direction;
  96.  };
  97.  
  98. void __regargs InitRandom(struct StormStruct *RP,ULONG Instance)
  99.  
  100. {
  101.  ULONG Time[2];
  102.  
  103.  CurrentTime (&Time[0],&Time[1]);
  104.  RP->RandN=(LONG)Time[0];
  105.  
  106.  if (Time[1]<1024L) Time[1]|=1;
  107.  else Time[1]>>=10;
  108.  Time[1]^=Instance;
  109.  
  110.  RP->RandF=4*Time[1]+1;
  111.  RP->RandI=2*Time[1]+1;
  112. }
  113.  
  114. WORD __regargs Random(struct StormStruct *RP,WORD Max)
  115.  
  116. {
  117.  RP->RandN=RP->RandF*RP->RandN+RP->RandI;
  118.  if (RP->RandN<0L) RP->RandN=-RP->RandN;
  119.  
  120.  return (WORD)(RP->RandN%Max);
  121. }
  122.  
  123.  
  124. ULONG FindTagData(struct TagItem *TagList,Tag ID,ULONG Default)
  125.  
  126. {
  127.  while (TagList)
  128.   switch (TagList->ti_Tag)
  129.    {
  130.     case TAG_DONE:
  131.      return Default;
  132.     case TAG_MORE:
  133.      TagList=(struct TagItem *)TagList->ti_Data;
  134.      break;
  135.     default:
  136.      if (TagList->ti_Tag==ID) return TagList->ti_Data;
  137.      else TagList++;
  138.    }
  139.  return Default;
  140. }
  141.  
  142.  
  143. LONG MyBlankerLibInit(void)
  144.  
  145. {
  146.  if (GfxBase=(struct GfxBase *)OpenLibrary("graphics.library",37L))
  147.   {
  148.    if (IntuitionBase=(struct IntuitionBase *)OpenLibrary("intuition.library",37L))
  149.     {
  150.      if (UtilityBase=OpenLibrary("utility.library",37L)) return TRUE;
  151.      CloseLibrary (&IntuitionBase->LibNode);
  152.     }
  153.    CloseLibrary (&GfxBase->LibNode);
  154.   }
  155.  return FALSE;
  156. }
  157.  
  158. void MyBlankerLibFree(void)
  159.  
  160. {
  161.  CloseLibrary (UtilityBase);
  162.  CloseLibrary (&IntuitionBase->LibNode);
  163.  CloseLibrary (&GfxBase->LibNode);
  164. }
  165.  
  166. struct BTDInfo *QueryMyBlanker(void)
  167. {
  168.  return &StormInfo;
  169. }
  170.  
  171. struct StormStruct *InitMyBlanker(struct TagItem *TagList)
  172.  
  173. {
  174.  struct StormStruct *RP;
  175.  struct BTDDrawInfo *BTDDrawInfo;
  176.  ULONG *Error,Dummy,Instance,Index,Step;
  177.  
  178.  if ((BTDDrawInfo=(struct BTDDrawInfo *)
  179.                    FindTagData(TagList,BTD_DrawInfo,NULL))==NULL) return NULL;
  180.  Error=(LONG *)FindTagData(TagList,BTD_Error,(ULONG)&Dummy);
  181.  if ((RP=AllocVec(sizeof(struct StormStruct),MEMF_PUBLIC|MEMF_CLEAR))==NULL)
  182.   {
  183.    *Error=BTDERR_Memory;
  184.    return NULL;
  185.   }
  186.  
  187.  RP->BTDDrawInfo=BTDDrawInfo;
  188.  Instance=FindTagData(TagList,BTD_Instance,0L);
  189.  
  190.  InitRandom(RP,Instance);
  191.  
  192.  RP->Colors=FindTagData(TagList,RP_Colors,DEF_COLORS);
  193.  RP->Drops=FindTagData(TagList,RP_Drops,DEF_DROPS);
  194.  RP->Mode=FindTagData(TagList,RP_Mode,DEF_MODES);
  195.  RP->Direction=Random(RP,2)-1;
  196.  
  197.  RP->DropCount=0L;
  198.  
  199.  Step=(128/(RP->Colors));
  200.  
  201.  for(Index=0; Index<RP->Colors; Index++)
  202.   {
  203.    BTDDrawInfo->BDI_Red[BTDDrawInfo->BDI_Pens[Index]]=64+Step*Index;
  204.    BTDDrawInfo->BDI_Green[BTDDrawInfo->BDI_Pens[Index]]=64+Step*Index;
  205.    BTDDrawInfo->BDI_Blue[BTDDrawInfo->BDI_Pens[Index]]=64+Step*Index;
  206.    BTDDrawInfo->BDI_Changed[BTDDrawInfo->BDI_Pens[Index]]=TRUE;
  207.   }
  208.  
  209.  return RP;
  210. }
  211.  
  212. void EndMyBlanker(struct StormStruct *RP)
  213.  
  214. {
  215. DEBUG_PRINT("Storm: FreeMem\n");
  216.  FreeVec (RP);
  217. }
  218.  
  219. void AnimMyBlanker(struct StormStruct *RP)
  220.  
  221. {
  222.  
  223.   LONG Len,x,y,Index;
  224.   struct RastPort *R;
  225.   struct BTDDrawInfo *BTD;
  226.  
  227.   BTD=RP->BTDDrawInfo;
  228.   R=BTD->BDI_RPort;
  229.  
  230.   if(RP->DropCount++ >RP->Drops)
  231.    {
  232.     /* Lightning */
  233.  
  234.     if(RP->Mode && Random(RP,100)>80)
  235.      {
  236.       SetAPen(R,BTD->BDI_Pens[Random(RP,RP->Colors)]);
  237.       RectFill (R,
  238.               BTD->BDI_Left,
  239.               BTD->BDI_Top,
  240.               BTD->BDI_Left+BTD->BDI_Width-1,
  241.               BTD->BDI_Top+BTD->BDI_Height-1);
  242.      }
  243.     WaitTOF();
  244.     SetAPen(R,BTD_BgPen);
  245.     RectFill (R,
  246.               BTD->BDI_Left,
  247.               BTD->BDI_Top,
  248.               BTD->BDI_Left+BTD->BDI_Width-1,
  249.               BTD->BDI_Top+BTD->BDI_Height-1);
  250.     RP->DropCount=0L;
  251.  
  252.     if(Random(RP,100)<15) 
  253.      {
  254.       RP->Direction=Random(RP,4);
  255.      }
  256.  
  257.    }
  258.  
  259.    Len = Random(RP, (BTD->BDI_Width-1)/18)+(BTD->BDI_Width-1)/35;
  260.    x = Random(RP,(BTD->BDI_Width-1)-2*Len)+Len+BTD->BDI_Left-1;
  261.    y = Random(RP,(BTD->BDI_Height-1)-2*Len)+Len+BTD->BDI_Top-1;
  262.  
  263.    switch(RP->Direction) {
  264.    case 0: /* links */
  265.    for(Index=0; Index<Len; Index++)
  266.     {
  267.       SetAPen(R,BTD->BDI_Pens[Index%RP->Colors]);
  268.       WritePixel(R,x-Index,y+Index);
  269.     }
  270.    break;
  271.    case 1:
  272.    for(Index=0; Index<Len; Index++)
  273.     {
  274.       SetAPen(R,BTD->BDI_Pens[Index%RP->Colors]);
  275.       WritePixel(R,x+Index,y+Index);
  276.     }
  277.    break;
  278.    case 2:
  279.    for(Index=Len; Index>0; Index--)
  280.     {
  281.       SetAPen(R,BTD->BDI_Pens[Index%RP->Colors]);
  282.       WritePixel(R,x,y+Index);
  283.     }
  284.    break;
  285.    case 3:
  286.    for(Index=Len; Index>0; Index--)
  287.     {
  288.       SetAPen(R,BTD->BDI_Pens[Index%RP->Colors]);
  289.       WritePixel(R,x+Index,y+Index/2);
  290.     }
  291.    break;
  292.    case 4:
  293.    for(Index=0; Index<Len; Index++)
  294.     {
  295.       SetAPen(R,BTD->BDI_Pens[Index%RP->Colors]);
  296.       WritePixel(R,x-Index,y+Index/2);
  297.     }
  298.    }
  299. }
  300.  
  301. ULONG PenCountMyBlanker(struct TagItem *TagList)
  302.  
  303. {
  304.  return FindTagData(TagList,RP_Colors,DEF_COLORS);;
  305. }
  306.